package com.clkj.cldisabledmanclient.mvp.myapply;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.clkj.cldisabledmanclient.R;
import com.clkj.cldisabledmanclient.adapter.PhotoAdapter;
import com.clkj.cldisabledmanclient.app.CustomApplication;
import com.clkj.cldisabledmanclient.base.BaseActivity;
import com.clkj.cldisabledmanclient.bean.MyApplyDetail;
import com.clkj.cldisabledmanclient.util.ToastUtil;
import com.clkj.cldisabledmanclient.util.scheduler.SchedulerProvider;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

public class MyApplyDetailActivity extends BaseActivity implements MyApplyDetailContract.View {

    private RelativeLayout mRlTitle;
    private ImageView mIvBack;
    private TextView mTvTitle;
    private TextView mTvRight;
    private ImageView mIvApply;
    private TextView mTvName;
    private TextView mTvSex;
    private TextView mTvBirthday;
    private TextView mTvIdcard;
    private TextView mTvAddress;
    private TextView mTvCanjikind;
    private ListView mLvFdjhr;
    private TextView mTvFamilyKind;
    private TextView mTvFamilyResource;
    private TextView mTvFamilySalary;
    private TextView mTvApplyReason;
    private TextView mTvReCoverOrg;
    private RecyclerView mRvPhotos;
    private TextView mTvPeriod;
    private LinearLayout mLyGuardian;
    private TextView mTvGuardianName;
    private TextView mTvGuardianRelationship;
    private TextView mTvGuardianCompany;
    private TextView mTvGuardianTel;
    private LinearLayout mLyGuardian2;
    private TextView mTvGuardianName2;
    private TextView mTvGuardianRelationship2;
    private TextView mTvGuardianCompany2;
    private TextView mTvGuardianTel2;

    private List<String> photos = new ArrayList<>();

    private PhotoAdapter photoAdapter;

    private MyApplyDetail myApplyDetail;
    private String personnelId;
    private ProgressDialog progressDialog;

    private MyApplyDetailContract.Presenter mPresenter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_apply_detail);
        initTitle(null, null, "我的申请详情", true, null);
        initData();
        initView();
        assignView();

        getMyApplyDetail();
    }

    @Override
    public void initData() {

        personnelId = getIntent().getStringExtra("personnelId");
        mPresenter = new MyApplyDetailPresenter(this, SchedulerProvider.getInstance(), CustomApplication.getInstance().getHttpService());
    }

    @Override
    public void assignView() {

        GridLayoutManager llManager = new GridLayoutManager(this, 3);
        //LinearLayoutManager llManager = new LinearLayoutManager(this);
        llManager.setOrientation(LinearLayoutManager.VERTICAL);
        //解决scrollview和recycleview嵌套滑动冲突
        llManager.setSmoothScrollbarEnabled(true);
        llManager.setAutoMeasureEnabled(true);

        mRvPhotos.setLayoutManager(llManager);
        mRvPhotos.setNestedScrollingEnabled(false);
        //如果可以确定每个item的高度是固定的，设置这个选项可以提高性能
        mRvPhotos.setHasFixedSize(true);

        photoAdapter = new PhotoAdapter(photos, this);
        mRvPhotos.setAdapter(photoAdapter);

        mRvPhotos.setHasFixedSize(true);

        /*if (postModel.getImgUrl().size()!=0) {
            int num = postModel.getImgUrl().size();
            photos.clear();
            for (int i = 0; i < num; i++) {
                photos.add(postModel.getImgUrl().get(i));
            }

            PhotoAdapter.notifyDataSetChanged();
        }*/

        //获取焦点，防止被recycleview抢占
        mTvName.setFocusable(true);
        mTvName.setFocusableInTouchMode(true);
        mTvName.requestFocus();
        mRvPhotos.setFocusable(false);
    }

    @Override
    public void initView() {

        mRlTitle = findViewById(R.id.rl_title);
        mIvBack = findViewById(R.id.iv_back);
        mTvTitle = findViewById(R.id.tv_title);
        mTvRight = findViewById(R.id.tv_right);
        mIvApply = findViewById(R.id.iv_apply);
        mTvName = findViewById(R.id.tv_name);
        mTvSex = findViewById(R.id.tv_sex);
        mTvBirthday = findViewById(R.id.tv_birthday);
        mTvIdcard = findViewById(R.id.tv_idcard);
        mTvAddress = findViewById(R.id.tv_address);
        mTvCanjikind = findViewById(R.id.tv_canjikind);
        mLvFdjhr = findViewById(R.id.lv_fdjhr);
        mTvFamilyKind = findViewById(R.id.tv_familyKind);
        mTvFamilyResource = findViewById(R.id.tv_familyResource);
        mTvFamilySalary = findViewById(R.id.tv_familySalary);
        mTvApplyReason = findViewById(R.id.tv_applyReason);
        mTvReCoverOrg = findViewById(R.id.tv_recoverOrg);
        mRvPhotos = findViewById(R.id.rv_photos);
        mTvPeriod = findViewById(R.id.tv_period);

        mLyGuardian = findViewById(R.id.ly_guardian);
        mTvGuardianName = findViewById(R.id.tv_guardianName);
        mTvGuardianRelationship = findViewById(R.id.tv_guardianRelationship);
        mTvGuardianCompany = findViewById(R.id.tv_guardianCompany);
        mTvGuardianTel = findViewById(R.id.tv_guardianTel);
        mLyGuardian2 = findViewById(R.id.ly_guardian2);
        mTvGuardianName2 = findViewById(R.id.tv_guardianName2);
        mTvGuardianRelationship2 = findViewById(R.id.tv_guardianRelationship2);
        mTvGuardianCompany2 = findViewById(R.id.tv_guardianCompany2);
        mTvGuardianTel2 = findViewById(R.id.tv_guardianTel2);
    }

    @Override
    public void getMyApplyDetail() {

        mPresenter.getMyApplyDetail(personnelId);
    }

    @Override
    public void getMyApplyDetailSuccess(MyApplyDetail myApplyDetail) {

        this.myApplyDetail = myApplyDetail;

        //获取到即往视图里面插数据
        mTvName.setText("姓名：" + myApplyDetail.getName());
        mTvSex.setText("性别：" + myApplyDetail.getGender());
        mTvBirthday.setText("出生年月：" + myApplyDetail.getBirthday());
        mTvIdcard.setText("身份证号：" + myApplyDetail.getIdCard());
        mTvAddress.setText(myApplyDetail.getAddress());
        mTvCanjikind.setText(myApplyDetail.getDisabilityCategory());

        if (!TextUtils.isEmpty(myApplyDetail.getGuardianName())) {
            mLyGuardian.setVisibility(View.VISIBLE);
            mTvGuardianName.setText(myApplyDetail.getGuardianName());
            mTvGuardianRelationship.setText(myApplyDetail.getGuardianRelationship());
            mTvGuardianCompany.setText(myApplyDetail.getGuardianCompany());
            mTvGuardianTel.setText(myApplyDetail.getGuardianTel());
        }

        if (!TextUtils.isEmpty(myApplyDetail.getGuardianName2())) {
            mLyGuardian2.setVisibility(View.VISIBLE);
            mTvGuardianName2.setText(myApplyDetail.getGuardianName2());
            mTvGuardianRelationship2.setText(myApplyDetail.getGuardianRelationship2());
            mTvGuardianCompany2.setText(myApplyDetail.getGuardianCompany2());
            mTvGuardianTel2.setText(myApplyDetail.getGuardianTel2());
        }


        mTvFamilyKind.setText("类别：" + myApplyDetail.getFamilyCategory());
        mTvFamilyResource.setText("经济来源：" + myApplyDetail.getEconomicSource());
        mTvFamilySalary.setText("年人均收入：" + myApplyDetail.getPersonnelAnnualIncome());
        mTvApplyReason.setText(myApplyDetail.getReasonDemand());
        mTvReCoverOrg.setText(myApplyDetail.getInstitutionName());
        mTvPeriod.setText(myApplyDetail.getPersonnelCold());

        //个人头像
        if (!TextUtils.isEmpty(myApplyDetail.getPersonnelImgage())) {
            Picasso.get().load(myApplyDetail.getPersonnelImgage()).
                    placeholder(R.mipmap.icon_avator).error(R.mipmap.icon_avator).into(mIvApply);
        }

        photos.clear();
        List<String> pictures = new ArrayList<>();
        if (!TextUtils.isEmpty(myApplyDetail.getGuardianIDCard())) {
            pictures.add(myApplyDetail.getGuardianIDCard());
        }
        if (!TextUtils.isEmpty(myApplyDetail.getGuardianResidenceBooklet())) {
            pictures.add(myApplyDetail.getGuardianResidenceBooklet());
        }
        if (!TextUtils.isEmpty(myApplyDetail.getChildrenResidenceBooklet())) {
            pictures.add(myApplyDetail.getChildrenResidenceBooklet());
        }
        if (!TextUtils.isEmpty(myApplyDetail.getLowAssurance())) {
            pictures.add(myApplyDetail.getLowAssurance());
        }
        if (!TextUtils.isEmpty(myApplyDetail.getDisabilityCertificate())) {
            pictures.add(myApplyDetail.getDisabilityCertificate());
        }
        if (!TextUtils.isEmpty(myApplyDetail.getHospitalProve())) {
            pictures.add(myApplyDetail.getHospitalProve());
        }
        photos.addAll(pictures);
        photoAdapter.notifyDataSetChanged();
    }

    @Override
    public void getMyApplyDetailError(String tip) {

        ToastUtil.showShort(this, tip);
    }

    @Override
    public void setPresenter(MyApplyDetailContract.Presenter presenter) {

    }

    @Override
    public void showLoading() {
        if (progressDialog == null) {
            progressDialog = new ProgressDialog(this);
            progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            progressDialog.setIndeterminate(false);
            progressDialog.setCancelable(true);
            //设置点击进度对话框外的区域对话框不消失
            progressDialog.setCanceledOnTouchOutside(false);
            progressDialog.setMessage("正在加载...");
            progressDialog.show();
        }
    }

    @Override
    public void dismissLoading() {
        if (progressDialog != null) {
            progressDialog.dismiss();
            progressDialog = null;
        }
    }

}
